jQuery(function($){	
	/* Resize Video*/
	function resizeVideo() {
		jQuery("object, embed, iframe").each(function() {
			if(jQuery(this).parents('.jp-jplayer').length > 0)
				return false;

			var obj = jQuery(this);
			var ratio = obj.height() / obj.width();
			var parentWidth = parseInt(obj.parent().width());

			obj.width(parentWidth).height(parseInt(parentWidth * ratio));
		});
	}

	/* Resize jPlayer */
	function resizejPlayer() {
		jQuery(".jp-jplayer").each(function() {
			var obj = jQuery(this);

			if(obj.attr('data-type') != 'video')
				return false;
		
			var ratio = obj.attr('data-height') / obj.attr('data-width');
			var parentWidth = parseInt(obj.parent().width());
			var resizedHeight = parseInt(parentWidth * ratio);

			obj.width(parentWidth).height(resizedHeight).find('img, video, object, embed').width(parentWidth).height(resizedHeight);
		});
	}

	/* Resize jSlide */
	function resizeSlide() {
		jQuery('.slide').each(function(){
			jQuery(this).width(jQuery(this).parents('.slides_container').width());
			jQuery(this).parents('.slides_control').height(jQuery(this).height());
		});
	}

	/* Replace Menu to Select Box */
	function replaceMenu(menu) {
		if ($(window).width() <= 503) {
			var menuClone = menu.clone();
		
			menu.html('<div class="select-div"><div class="select-wrap"><select class="menu-select"></select></div></div>');
			var selectMenu = menu.find('.menu-select');

			if (!String.repeat) {
				String.prototype.repeat = function(l) {
					return new Array(l + 1).join(this);
				}
			}
		
			$(menuClone).find('li').each(function() {
				depth = $(this).parents('ul').length;

				var href = $(this).children('a').attr('href');
				var text = $(this).children('a').text();

				var prefix = '';
				if (depth > 1) {
					text = '--' + text;
				}
				if (depth > 2) {
					text = '&nbsp;&nbsp;'.repeat(depth - 1) + text;
				}
			
				var selected = '';
				if ($(this).hasClass('current-menu-item')) 
					selected = ' selected="selected"';

				$(selectMenu).append('<option value="' + href + '"' + selected + '>' + text + '</option>');
			});
		
			$(selectMenu).change(function() {
				location = this.options[this.selectedIndex].value;
			});
		} 
	};

	replaceMenu($('#main-nav'));
	replaceMenu($('.sub-nav'));

	jQuery(window).bind('load resize', function(){
		resizeSlide();
		resizejPlayer();
		resizeVideo();
		
		if($(window).width() >= 504 && $(window).width() <= 959)
			$('#sidebar-wrap').masonry({});
		if($(window).width() >= 504 && $(window).width() <= 755)
			$('#footbar .nag').masonry({});
	});
	
	/* Sticky Footer */
	$('#footer').stickyFooter();
		
	/* Toggle Placeholder */
	$('input[type="text"]').each(function(){
		var placeholder = $(this).attr('placeholder');
		
		$(this).bind('focus', function(){
			if($(this).attr('value') == '')
				$(this).attr('value', '').attr('placeholder', '');
		}).bind('blur', function(){
			if($(this).attr('value') == '')
				$(this).attr('placeholder', placeholder);
		});
	});
	
	/* Slider */
	if(jQuery().slides) {
	jQuery(".slider img").imagesLoaded(function() {
		// $(this).parents('.slide').width($(this).parents('.slides_container').width());
		
		jQuery(".slider").slides({
			generatePagination: false,
			pagination: true,
			paginationClass: 'slider-thumbs',
			generateNextPrev: true,
			next: 'next',
			prev: 'prev',
			effect: 'fade',
			crossfade: true,
			autoHeight: true,
			bigTarget: true
		});
		
		$('.slider .slide').each(function(){
			var index = $('.slide').index(this);
			var image = $(this).find('img');
		});
		
		if(jQuery().hoverscroll) {
		$('.slider-thumbs').hoverscroll({
			width: '',
			height: ''
		});
		}
	});
	}

	/* Remove the dotted outline */
	$('a,input[type="submit"]').bind('focus',function(){if(this.blur){ this.blur();}});
	
	/* Work - Toggle sub nav */
	$('.sub-nav-toggle').click(function() {
		if($('.sub-nav').css('display') == 'block') {
			$(this).removeClass('sub-nav-toggle-active');
			$('.sub-nav').slideUp(function(){$('.sub-nav').addClass('hide');});
			
			$.cookie('sub-nav', 'hide', { path: '/'});
		} else {
			$(this).addClass('sub-nav-toggle-active');
			$('.sub-nav').slideDown(function(){$('.sub-nav').removeClass('hide');});
			
			$.cookie('sub-nav', 'show', { path: '/', expires: 10});
		}
		
		return false;
	});
});